//
//  INAcountDetialViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/30.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "INAcountDetialViewController.h"
#import "AcountDetialInModel.h"
#import "LookInforTableViewCell.h"
#import "RSTableHeadView.h"
#import "RegisterMorePicTableViewCell.h"
#import "RegisterSpicTableViewCell.h"
#import "INCountDetialTableViewCell.h"
#import "ExaminationTableViewCell.h"
#import "LookInforViewController.h"
@interface INAcountDetialViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView *homeTableView;

@property(nonatomic,strong)AcountDetialInModel *inforModel;

@property(nonatomic,copy)NSString*headimage;

@end

@implementation INAcountDetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isScanphotos=YES;
    
    [self.view addSubview:self.homeTableView];
    
    [self loadDatas];
    // Do any additional setup after loading the view.
}


-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        
        _homeTableView.estimatedRowHeight=44;
        
        _homeTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        [self configureNeedShowEmptyDataSetScrollView:_homeTableView];
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"LookInforTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        [_homeTableView registerClass:[RegisterMorePicTableViewCell class] forCellReuseIdentifier:@"TypeCell3"];
        
          [_homeTableView registerNib:[UINib nibWithNibName:@"INCountDetialTableViewCell" bundle:nil] forCellReuseIdentifier:@"TypeCell4"];
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"RegisterSpicTableViewCell" bundle:nil] forCellReuseIdentifier:@"TypeCell5"];
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"ExaminationTableViewCell" bundle:nil] forCellReuseIdentifier:@"ExaminationCell"];
        
        [_homeTableView registerClass:[RSTableHeadView class] forHeaderFooterViewReuseIdentifier:@"head"];
        
        _homeTableView.tableFooterView=[[UIView alloc]init];
        
        
    }
    
    return _homeTableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1+self.inforModel.data.details.count;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        
        return self.inforModel.data.list.count;
        
    }else{
        
        return 1;
        
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section >0)
    {
        AcountInDetails *detial=self.inforModel.data.details[indexPath.section-1];
        
        LookInforViewController *Infor=[LookInforViewController new];
        
        Infor.reqUrl=@"/index.php/FoodCheckApi/ledgerItemDetail.json";
        
        Infor.params=@{@"detailId":[NSString stringWithFormat:@"%ld",detial.detailId]};
        
        Infor.title=@"票证信息";
        
        [self.navigationController pushViewController:Infor animated:YES];
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        AcountInList *list=self.inforModel.data.list[indexPath.row];
        
        if (list.fieldType==4) {
            
            RegisterSpicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TypeCell5" forIndexPath:indexPath];
            
            cell.TitleName=list.name;
            
            if (list.value.length==0) {
                
                cell.IcoImageView.userInteractionEnabled=NO;
                
            }else{
                
                cell.IcoImageView.userInteractionEnabled=YES;
                
                self.headimage=list.value;
                
                UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ShowHEAD)];
                
                [cell.IcoImageView addGestureRecognizer:tap];
                
            }
            
            [cell.IcoImageView LoadWebImageWithName:list.value andIsOrgin:NO];
            
            return cell;
            
        }else if (list.fieldType==5)
        {
            
            RegisterMorePicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TypeCell3" forIndexPath:indexPath];
            
            cell.TitleName=list.name;
            
            cell.isEdit=NO;//是否可编辑
            
            cell.ImageArr=[HelpWay imageStrToArr:list.value];//传递图片数组
            
              [cell updateLebeframe];
            
            TBWeak;
            //浏览图片
            cell.ScanBlock=^(NSInteger num)
            {
                [weakSelf.ScanPhotoArr removeAllObjects];
                
                for (NSString*imagestr in [HelpWay imageStrToArr:list.value]) {
                    
                    [weakSelf.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,imagestr]]]];
                    
                }
                
                [weakSelf GoToPhotoScanAndIndex:num];
                
            };
            
            return cell;
            
        }else{
            
            LookInforTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
            
            cell.AList=list;
            
            return cell;
            
        }
        
    }else{
        
        AcountInDetails *detial=self.inforModel.data.details[indexPath.section-1];
        
        if (indexPath.row<4) {
            
            ExaminationTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ExaminationCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell refreshDetailData:detial];
            
            return cell;
            
//            INCountDetialTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TypeCell4" forIndexPath:indexPath];
//
//            switch (indexPath.row) {
//                case 0:
//                {
//                    cell.OneLabel.text=[NSString stringWithFormat:@"产品条码:%@",detial.productSn!=nil?detial.productSn:@""];
//
//                    cell.twoLabel.text=[NSString stringWithFormat:@"品名:%@",detial.goodsName!=nil?detial.goodsName:@""];
//
//
//                }
//                    break;
//                case 1:
//                {
//                    cell.OneLabel.text=[NSString stringWithFormat:@"数量:%@",detial.quantity!=nil?detial.quantity:@""];
//
//                    cell.twoLabel.text=[NSString stringWithFormat:@"单位:%@",detial.unit!=nil?detial.unit:@""];
//                }
//                    break;
//
//                case 2:
//                {
//                    CGFloat totalPrice=[detial.price floatValue]*[detial.quantity floatValue];
//
//                    cell.OneLabel.text=[NSString stringWithFormat:@"小计:%.2f",totalPrice];
//
//                    cell.twoLabel.text=[NSString stringWithFormat:@"单价:%@",detial.price!=nil?detial.price:@""];
//                }
//                    break;
//                case 3:
//                {
//
//                    cell.OneLabel.text=[NSString stringWithFormat:@"生产日期:%@", [HelpWay timestampChangesStandarTime:[NSString stringWithFormat:@"%ld",detial.birthTime]]];
//
//                    cell.twoLabel.text=[NSString stringWithFormat:@"保质期:%@",detial.period.length!=0?detial.period:@""];
//                }
//                    break;
//
//                default:
//                    break;
//            }
//
//
//            return cell;
            
        }else{
            
            RegisterMorePicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TypeCell3" forIndexPath:indexPath];
            
            cell.TitleName=@"检验报告:";
            
            cell.isEdit=NO;//是否可编辑
            
            cell.ImageArr=[HelpWay imageStrToArr:detial.qcReport];//传递图片数组
            
              [cell updateLebeframe];
            
            TBWeak;
            //浏览图片
            cell.ScanBlock=^(NSInteger num)
            {
                [weakSelf.ScanPhotoArr removeAllObjects];
                
                for (NSString*imagestr in [HelpWay imageStrToArr:detial.qcReport]) {
                    
                    [weakSelf.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,imagestr]]]];
                    
                }
                
                [weakSelf GoToPhotoScanAndIndex:num];
                
            };
            
            return cell;
    
        }
  
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        AcountInList *list=self.inforModel.data.list[indexPath.row];
        
        if (list.fieldType==4) {
            
            return 60;
            
        }else if (list.fieldType==5)
        {
            NSArray *arr=[HelpWay imageStrToArr:list.value];
            
            if (arr.count==0) {
                
                return 43;
                
            }else{
                
                NSInteger colorm=(arr.count-1)/3+1;//每行放三个，一个有多少行
                
                CGFloat cellHeight=(SCREEN_WIDTH-30-10*2)/3.0;
                
                return 15+10*(colorm-1)+colorm*cellHeight+47;
                
            }
        }
          return UITableViewAutomaticDimension;
    }else{
        
         AcountInDetails *detial=self.inforModel.data.details[indexPath.section-1];
        
        if (indexPath.row<4) {
            
            return UITableViewAutomaticDimension;
            
        }else{
            
            NSArray *arr=[HelpWay imageStrToArr:detial.qcReport];
            
            if (arr.count==0) {
                
                return 43;
                
            }else{
                
                NSInteger colorm=(arr.count-1)/3+1;//每行放三个，一个有多少行
                
                CGFloat cellHeight=(SCREEN_WIDTH-30-10*2)/3.0;
                
                return 15+10*(colorm-1)+colorm*cellHeight+47;
                
            }
        }
 
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0||section==1) {
        
        return 30;
    }
    return 1;
    
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  
        
        RSTableHeadView *head=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
        
        if (section==0) {
            
             head.Rtitle=self.inforModel.data.title;
            
        }else if (section==1)
        {
            
             head.Rtitle=@"商品信息";
            
        }else{
            
            head.Rtitle=@"";
        }

        return head;

}

-(void)ShowHEAD
{
    [self.ScanPhotoArr removeAllObjects];
    
    [self.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,self.headimage]]]];
    
    [self GoToPhotoScanAndIndex:0];
    
}

-(void)loadDatas
{
    [self showLoadingAnimation];

    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:self.reqUrl andrequestTime:5 andParams:self.params andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];

    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {

        [self stopLoadingAnimation];

        self.inforModel=[AcountDetialInModel mj_objectWithKeyValues:responseObject];

        if (responseStatus==1) {
            //请求成功
            
            if (self.inforModel.data==nil) {

                self.Type=TBNODateType;

            }else{

                [self.homeTableView reloadData];

            }
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];

            self.Type=TBReqFalie;

        }else{
            //404需要重新登录了

            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {

                if (buttonIndex==1) {

                    [self GotoLoginVC];
                }

            }];
        }

    }];

}

- (void)actionTapEmptyDataSetNodataView
{
    [self loadDatas];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
